//
//  MOMyGetMarriedViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyGetMarriedViewController.h"
//结亲记录
#import "MOMyGetMarriedOfHisViewController.h"
//创建结亲计划
#import "MOMyGetMarriedPlanEditOrCreatViewController.h"
//创建结亲对象
#import "MOMyGetMarriedProjectEditOrCreatViewController.h"

@interface MOMyGetMarriedViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation MOMyGetMarriedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
    [self initData];
}

-(void)initNav
{
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
    [addButton setImage:IMAGE(@"use_add") forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(clickAddMarried) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

-(void)creatView
{
    self.title = @"我的结亲";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.tableView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(initData) name:@"hisAction" object:nil];

}

-(void)initData
{
    NSString *url = [NSString stringWithFormat:@"%@?userId=%@&authorization=%@",RECIPIENT_GETRECIPIENTSBYUSERID_URL,[MOUser sharedMOUser].userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{} success:^(id responseObject) {
        NSLog(@"结亲对象列表：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSArray class]]) {
            NSArray *arr = responseObject;
            self.dataSource = [NSMutableArray array];
            for (int i = 0; i<arr.count; i++) {
                [self.dataSource addObject:arr[i]];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲对象列表：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
}


#pragma mark -- 结亲
-(void)clickAddMarried
{
    MOMyGetMarriedProjectEditOrCreatViewController *vc = [[MOMyGetMarriedProjectEditOrCreatViewController alloc]init];
    __weak typeof (self)weakSelf = self;
    vc.editOrCreatSucessBlock = ^{
        [weakSelf initData];
    };
    vc.editOrCreatStatus = @"2";
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)copyMarriedForDate:(NSString *)date
{
    NSString *str = [[date substringWithRange:NSMakeRange(0, 4)] stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSString *current = [NSString getCurrentTimesAndFormat:@"YYYY"];
    if ([current integerValue]==[str integerValue]) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"同一年不能存在两个相同结亲对象，请进行其他操作！" showInView:self.view];
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENT_COPYRECIPIENT_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{} success:^(id responseObject) {
        NSLog(@"结亲对象复制：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
    } failure:^(NSError *error) {
        NSLog(@"结亲对象复制：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
    }
    
    NSString *str = [[self.dataSource[indexPath.row][@"createTime"] substringWithRange:NSMakeRange(0, 4)] stringByReplacingOccurrencesOfString:@"-" withString:@""];
    cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)\t\t进度:%@%@",self.dataSource[indexPath.row][@"masterName"],str,self.dataSource[indexPath.row][@"percent"],@"%"];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 13;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (@available(iOS 8.0, *)) {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
        
        NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"请选择"];
        [alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:KBlueColor_2C94FF range:NSMakeRange(0, 3)];
        [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 3)];
        [actionSheet setValue:alertControllerMessageStr forKey:@"attributedMessage"];
        
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
        }];
        
        UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"复制结亲对象" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self selectedIndex:@"3" indexPath:indexPath];
            
        }];
        
        UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"结亲计划" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            [self selectedIndex:@"2" indexPath:indexPath];
        }];
        
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"结亲记录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            [self selectedIndex:@"1" indexPath:indexPath];
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"编辑结亲对象" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self selectedIndex:@"0" indexPath:indexPath];
            
        }];
        
        [actionSheet addAction:action1];
        [actionSheet addAction:action2];
        [actionSheet addAction:action3];
        [actionSheet addAction:action4];
        [actionSheet addAction:action5];
        
        [self presentViewController:actionSheet animated:YES completion:nil];
        
    }else{
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    return;
    SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    NSArray *tArr = @[@"编辑结亲对象",@"结亲记录",@"结亲计划",@"复制结亲对象"];
    pickview.numberArray = tArr;
    __weak typeof (self)weakSelf = self;
    pickview.clickIndexBlock = ^(NSInteger index) {
        if (index==0) {
            MOMyGetMarriedProjectEditOrCreatViewController *vc = [[MOMyGetMarriedProjectEditOrCreatViewController alloc]init];
            __weak typeof (self)weakSelf = self;
            vc.editOrCreatSucessBlock = ^{
                [weakSelf initData];
            };
            vc.projectDic = self.dataSource[indexPath.row];
            vc.editOrCreatStatus = @"1";
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else if (index==1){
            MOMyGetMarriedOfHisViewController *vc = [[MOMyGetMarriedOfHisViewController alloc]init];
            vc.masterId = self.dataSource[indexPath.row][@"masterId"];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else if (index==2){
            MOMyGetMarriedPlanEditOrCreatViewController *vc = [[MOMyGetMarriedPlanEditOrCreatViewController alloc]init];
            vc.masterId = self.dataSource[indexPath.row][@"masterId"];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else if (index==3){
            [weakSelf copyMarriedForDate:self.dataSource[indexPath.row][@"createTime"]];
        }
    };
    [WINDOW addSubview:pickview];
}

-(void)selectedIndex:(NSString *)selectedIndex indexPath:(NSIndexPath *)indexPath;
{
    NSInteger index = [selectedIndex integerValue];
    if (index==0) {
        MOMyGetMarriedProjectEditOrCreatViewController *vc = [[MOMyGetMarriedProjectEditOrCreatViewController alloc]init];
        __weak typeof (self)weakSelf = self;
        vc.editOrCreatSucessBlock = ^{
            [weakSelf initData];
        };
        vc.projectDic = self.dataSource[indexPath.row];
        vc.editOrCreatStatus = @"1";
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }else if (index==1){
        MOMyGetMarriedOfHisViewController *vc = [[MOMyGetMarriedOfHisViewController alloc]init];
        vc.masterId = self.dataSource[indexPath.row][@"masterId"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==2){
        MOMyGetMarriedPlanEditOrCreatViewController *vc = [[MOMyGetMarriedPlanEditOrCreatViewController alloc]init];
        vc.masterId = self.dataSource[indexPath.row][@"masterId"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index==3){
        [self copyMarriedForDate:self.dataSource[indexPath.row][@"createTime"]];
    }
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}
@end
